package com.moleq.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;

import com.moleq.base.MoleQBaseDialog;
import com.moleq.logic.TransactionManager;
import com.moleq.mgpos.activities.R;
import com.moleq.mgpos.widget.NumberPad;
import com.moleq.mgpos.widget.NumberPad.NumberPadListener;
import com.moleq.mgpos.widget.NumberPadButton;
import com.moleq.service.ServiceManager;
import com.moleq.service.UsersService;

public class LoginDialog extends MoleQBaseDialog {
	private NumberPad numberPanel;
	private EditText et_login_id, et_password;
	private Button btn_dialog_ok, btn_dialog_back;
	private UsersService mUsersService;

	public LoginDialog(Context context) {
		super(context, context);
		constructor(context);

	}

	public LoginDialog(Context context, int theme) {
		super(context, theme);
		constructor(context);
	}

	private void constructor(Context context) {
		mContext = context;
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.dialog_perview_login);
		this.setCancelable(false);

		mUsersService = ServiceManager.getInstance().mUsersService;

		numberPanel = (NumberPad) this.findViewById(R.id.numberPadLogin);
		numberPanel.setStyle(NumberPad.APPROVAL_STYLE);
		numberPanel.setNumberPadListener(numberPadListener);

		et_login_id = (EditText) this.findViewById(R.id.et_login_id);
		et_password = (EditText) this.findViewById(R.id.et_password);
		btn_dialog_ok = (Button) this.findViewById(R.id.btn_dialog_ok);
		btn_dialog_back = (Button) this.findViewById(R.id.btn_dialog_back);

		et_login_id.setOnTouchListener(onTouchListener);
		et_password.setOnTouchListener(onTouchListener);
		btn_dialog_ok.setOnClickListener(mOnClickListener);
		btn_dialog_back.setOnClickListener(mOnClickListener);

		setFoucesByControl(this.et_login_id);
		numberPanel.setInputEditText(et_login_id);
		et_login_id.requestFocus();

	}
	
	public boolean validation(){
		
		String usrId = et_login_id.getText().toString().trim();
		if (usrId.equals("") || usrId.equals(".")) {
			numberPanel.setInputEditText(et_login_id);
			setFoucesByControl(et_login_id);
//			MessageBox.show(mContext, "Invalid login id");
			return false;
		}

		String usrPwd = et_password.getText().toString().trim();
		if (usrPwd.equals("") || usrPwd.equals(".")) {
			numberPanel.setInputEditText(et_password);
			setFoucesByControl(et_password);
//			MessageBox.show(mContext, "Invalid login password");
			return false;
		}
		
		return true;
	}

	private NumberPadListener numberPadListener = new NumberPadListener() {

		@Override
		public void onClick(NumberPadButton button) {
			// TODO Auto-generated method stub
			switch (button) {
			case ENTER:
				
				if (!validation()) {
					return;
				}
				
				String usrId = et_login_id.getText().toString().trim();
//				if (usrId.equals("") || usrId.equals(".")) {
//					numberPanel.setInputEditText(et_login_id);
//					setFoucesByControl(et_login_id);
//					return;
//				}

				String usrPwd = et_password.getText().toString().trim();
//				if (usrPwd.equals("") || usrPwd.equals(".")) {
//					numberPanel.setInputEditText(et_password);
//					setFoucesByControl(et_password);
//					return;
//				}

				boolean result = mUsersService.CheckIsManager(usrId, usrPwd);
				if (result) {
					TransactionManager.getInstance().managerID = usrId;
				}
				onCloseWithReturn(R.id.btn_dialog_ok, String.valueOf(result));
				break;
				
			}
		}

	};

	private OnTouchListener onTouchListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {

			EditText ed = new EditText(mContext);
			int inType = ed.getInputType();
			ed.setInputType(InputType.TYPE_NULL);
			ed.onTouchEvent(event);
			ed.setInputType(inType);
			ed.setSelection(ed.getText().length());

			switch (v.getId()) {
			case R.id.et_login_id:

				if (et_password.isFocused()) {
					et_password.clearFocus();
				}

				setFoucesByControl(et_login_id);
				numberPanel.setInputEditText(et_login_id);
				// etLoginId.requestFocus();
				break;
			case R.id.et_password:
				if (et_login_id.isFocused()) {
					et_login_id.clearFocus();
				}
				setFoucesByControl(et_password);
				numberPanel.setInputEditText(et_password);
				// etPwd.requestFocus();
				break;
			default:
				//
				break;
			}
			return true;
		}
	};

	Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			switch (view.getId()) {
			case R.id.btn_dialog_ok:
				
				if (!validation()) {
					return;
				}
				String usrId = et_login_id.getText().toString().trim();
//				if (usrId.equals("") || usrId.equals(".")) {
//					numberPanel.setInputEditText(et_login_id);
//					setFoucesByControl(et_login_id);
//					return;
//				}

				String usrPwd = et_password.getText().toString().trim();
//				if (usrPwd.equals("") || usrPwd.equals(".")) {
//					numberPanel.setInputEditText(et_password);
//					setFoucesByControl(et_password);
//					return;
//				}

				boolean result = mUsersService.CheckIsManager(usrId, usrPwd);
				onCloseWithReturn(view.getId(), String.valueOf(result));
				break;
			case R.id.btn_dialog_back:
				dismiss();
			default:
				break;
			}
		}

	};

	private void setFoucesByControl(EditText et) {
		et.setSelected(true);
		et.setFocusable(true);
		et.setFocusableInTouchMode(true);
		et.requestFocus();
	}

	public void show() {
		super.show();
		setFoucesByControl(et_login_id);
		numberPanel.setInputEditText(et_login_id);
		et_login_id.setText("");
		et_password.setText("");
	}
}
